home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_075 / comm / phonelib.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  168 lines

  1. /*  Comm phone library routines */
  2.  
  3. #define  PHONE 1
  4.  
  5. #include "globals.h"
  6.  
  7. extern void emits_tx(), emits_rx();
  8. extern UBYTE *strncpy();
  9. extern UBYTE *index();
  10. extern struct MenuItem PhoneItem[];
  11.  
  12. #define strchr(s,c)  index(s,c)
  13. #define PLEN   32
  14. #define SLEN   32
  15. #define HLEN   32
  16. #define ELEN   32
  17. #define ILEN   32
  18.  
  19. static UBYTE prefix[ PLEN ] = "ATDT";
  20. static UBYTE suffix[ SLEN ] = "\\r";
  21. static UBYTE hangup[ HLEN ] = "\\w\\w+++\\w\\w\\w\\wATH\\r";
  22.  
  23. UBYTE doinit[ ILEN ] = "";
  24. UBYTE doexit[ ELEN ] = "";
  25.  
  26. /*  determine which library to use
  27.     if no name given on command line, use default library PHONELIB
  28.     otherwise, use the one on the command line
  29. */
  30.  
  31. void Init_phone_lib()
  32. {
  33.     *alt_serv = NULL;
  34. }
  35.  
  36. /*  open phone library.  Return 0 if not found, else return # of numbers
  37.      in the library.
  38.     read library data into structure pdir.
  39. */
  40. int load_plib(fname)
  41. UBYTE *fname;
  42. {
  43.    int i,indx;
  44.    FILE *lib;
  45.    UBYTE *nl,temp[40],*file;
  46.  
  47.    static UBYTE pbuff[ MAXLINE ],
  48.                 *name    = &pbuff[ 0 ],
  49.                 *number  = &pbuff[ NAMESIZE ],
  50.                 *baud    = &pbuff[ NAMESIZE+NBRSIZE ],
  51.                 *comment = &pbuff[ NAMESIZE+NBRSIZE+BAUDSIZE ];
  52.  
  53.     strcpy(temp,install.DefDir);
  54.     strcat(temp,fname);
  55.  
  56.     file = fname;
  57.     if((lib = fopen(fname,"r")) == NULL) /* look in default dir */
  58.     {
  59.          file = temp;                /* nope: try default */
  60.          if((lib = fopen(temp,"r")) == NULL)
  61.            return 0;                 /* not in default either, return */
  62.     }
  63.  
  64.    strncpy(phonedir,file,MAXFNAME-1);  phonedir[MAXFNAME-1] = NULL;
  65.  
  66.    for(i = 0; i < MAXPHONE; i++)
  67.          pdir[i].number[0] = pdir[i].name[0] = NULL;
  68.  
  69.    i = 0;
  70.    while (i < MAXPHONE)
  71.    {
  72.       if(fgets(pbuff, MAXLINE, lib) == NULL)  /* read line from library */
  73.          break;
  74.       if( nl = strchr(pbuff,'\n'))            /* remove \n from line */
  75.          *nl = NULL;
  76.       indx = strlen(pbuff);
  77.  
  78.       while( indx < MAXLINE )                /* zero out unused fields */
  79.          pbuff[indx++] = NULL;
  80.  
  81.       if(pbuff[0] == '<')                    /* a new modem prefix ? */
  82.       {
  83.          switch( toupper(pbuff[1]) )
  84.          {
  85.              case 'D':
  86.              case 'P':   pbuff[ 2+PLEN ] = NULL;
  87.                          strcpy(prefix,&pbuff[2]); break;
  88.              case 'T':
  89.              case 'S':   pbuff[ 2+SLEN ] = NULL;
  90.                          strcpy(suffix,&pbuff[2]); break;
  91.              case 'H':   pbuff[ 2+HLEN ] = NULL;
  92.                          strcpy(hangup,&pbuff[2]); break;
  93.              case 'E':   pbuff[ 2+ELEN ] = NULL;
  94.                          strcpy(doexit,&pbuff[2]); break;
  95.              case 'I':   pbuff[ 2+ILEN ] = NULL;
  96.                          strcpy(doinit,&pbuff[2]); break;
  97.          }
  98.          continue;      /* OK, go get another line */
  99.       }
  100.       strncpy(pdir[i].name,   name   ,NAMESIZE);
  101.       strncpy(pdir[i].number, number ,NBRSIZE);
  102.       strncpy(pdir[i].baud,   baud   ,BAUDSIZE);
  103.       strncpy(pdir[i].comment,comment,COMTSIZE);
  104.  
  105.       if( nl == NULL)     /* if NULL, line extended beyond MAXLINE */
  106.         fgets(pbuff, MAXLINE, lib);       /* discard  rest of line */
  107.  
  108.       if(pdir[i].name[0] == '|' || pdir[i].name[0] == NULL)
  109.          continue;        /* ignore empty lines and our comment lines */
  110.       if(pdir[i].name[0] == '>')     /* check for alternate service */
  111.       {
  112.          strcpy(alt_serv, pdir[i].number);
  113.          continue;                /* overwrite entry */
  114.       }
  115.       i++;
  116.    }
  117.    fclose(lib);
  118.    if(i != 0)
  119.    return (i);
  120. }
  121.  
  122. /*  list the contents of the phone library entry to the screen
  123. */
  124.  
  125. void list_plib(item)
  126. unsigned int item;
  127. {
  128.      sprintf(sbuff,"\n%s%s%s%s\n", pdir[item].name,
  129.           pdir[item].number, pdir[item].baud, pdir[item].comment );
  130.      emits_rx(sbuff);
  131. }
  132.  
  133. /*  Dial number.  Passed a pointer to a phone number string.
  134.      Append prefix needed to get modem to dial, then send the string
  135.       to the modem.
  136. */
  137.  
  138. void dial(num)
  139. char *num;
  140. {
  141.    int i;
  142.    unsigned char c;
  143.  
  144.    if(*num)
  145.    {
  146.       i = 0;
  147.       sendout(prefix);             /* start of dial string */
  148.       i = 0;
  149.       if(altservflg)               /* send alt. service # if required */
  150.         sendout(alt_serv);
  151.       while(c = *num++)            /* send phone number */
  152.         if(c == ' ' || c == '-') continue;
  153.           else         sendchar(c);
  154.       sendout(suffix);             /* terminate dial string */
  155.    }
  156.    altservflg = FALSE;
  157. }
  158.  
  159. /*  Hang up modem.
  160.        send hangup command string.
  161. */
  162.  
  163. hang_up()
  164. {
  165.    sendout(hangup);
  166. }
  167.  
  168.